//
//  BEOrderDetailController.swift
//  BEGlobalOiler
//
//  Created by KKING on 16/9/6.
//  Copyright © 2016年 Bullseye Energy pte ltd. All rights reserved.
//

import UIKit

private let TechsCellID: String = "BEAssignedTechsCellID"
private let HeadCellID: String = "BEOrderDetailHeadCellID"

class BEOrderDetailController: UITableViewController {
    
    var orderId: Int64 = 0
    var orderDetailViewModel: BEOrderDetailViewModel = BEOrderDetailViewModel()
    var techDataCount: Int = 0
    var isheaderopen: Bool = false
    
    //用于判断当前页面是否需要支付footer
    var ispayfooter: Int = 0
    
    
    /**
     类加载的时候调用，swift里默认不提示了。。。。
     */
    override class func initialize() {
        
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(BEOrderDetailController.pushPriceTotalController), name: Notification.Name(rawValue: PushPriceTotalController), object: nil)
        
        setupUI()
        loadData(orderId: orderId)
    }
    
    //通知方法
    @objc private func pushPriceTotalController(){
        self.navigationController?.pushViewController(BEPriceTotalController(), animated: true)
    }
    
    //移除通知
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    private func loadData(orderId: Int64){
        JFProgressHUD.showloading()
        orderDetailViewModel.loadOrderDetailData(orderId: orderId) { (success, tip) in
            if success {
                JFProgressHUD.showSuccessWithStatus(status: tip)
                DispatchQueue.main.async(execute: {
                    self.tableView.reloadData()
                })
            } else {
                JFProgressHUD.showInfoWithStatus(status: tip)
            }
        }
    }
    
    private func setupUI(){
        
        tableView.backgroundColor = COLOR_ALL_BG
        tableView.register(UINib(nibName: "BEAssignedTechsCell", bundle: nil), forCellReuseIdentifier: TechsCellID)
        tableView.register(UINib(nibName: "BEOrderDetailHeadCell", bundle: nil), forCellReuseIdentifier: HeadCellID)
        
        switch ispayfooter {
        //等于0的时候用confirm footer
        case 0:
            let confirmFooter = BEOrderDetailConfirmFooter.footer() as! BEOrderDetailConfirmFooter
            confirmFooter.orderId = orderId
            tableView.tableFooterView = confirmFooter
        case 1:
            tableView.tableFooterView = UIView()
        //等于13的时候用payfooter
        case 13:
            let payFooter = BEOrderDetailPayFooter.footer() as! BEOrderDetailPayFooter
            payFooter.orderId = orderId
            tableView.tableFooterView = payFooter
            
        default:
            tableView.tableFooterView = UIView()
        }
    }
}

//数据源和代理方法
extension BEOrderDetailController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return orderDetailViewModel.modelData.orderTalentRefVOList.count + 1
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.row == 0 {
            let headCell = tableView.dequeueReusableCell(withIdentifier: HeadCellID, for: indexPath as IndexPath) as! BEOrderDetailHeadCell

            headCell.detailModel = orderDetailViewModel.modelData
            headCell.openHeaderBlock = { (openButton: UIButton ,isopen: Bool) -> Bool in
                self.isheaderopen = isopen
                self.tableView.reloadData()
                if isopen{
//                    openButton.setTitle("   Close details of the work order", for: .normal)
                    openButton.setImage(UIImage(named:
                        "detail_up"), for: .normal)
                    return false
                }else{
//                    openButton.setTitle("   View details of the work order", for: .normal)
                    openButton.setImage(UIImage(named: "detail_down"), for: .normal)
                    return true
                }
            }
            headCell.selectionStyle = .none
            return headCell
        }
        
        let techCell = tableView.dequeueReusableCell(withIdentifier: TechsCellID, for: indexPath as IndexPath) as! BEAssignedTechsCell
        techCell.selectionStyle = .none
        
        let techModelList = orderDetailViewModel.modelData.orderTalentRefVOList
        if techModelList.count == 0 {
            return techCell
        }
        
        techCell.techModel = techModelList[indexPath.row - 1]

        return techCell
    }
    
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.row == 0 {
            return
        }
        let journeyVc = BELookJourneyController()
        let orderTechModel = orderDetailViewModel.modelData.orderTalentRefVOList[indexPath.row - 1]
        journeyVc.orderTalentId = orderTechModel.id!
        self.navigationController?.pushViewController(journeyVc, animated: true)
    }
    
    
    //三个高度,从xib中可以得到.
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

        if indexPath.row == 0{
            if isheaderopen == false {
                return 425
            }else{
                return 736
            }
        }else{
            return 75
        }
    }
}
